package com.dao.impl;
import java.io.Serializable;
import java.util.List;
import org.hibernate.Query;
import org.hibernate.SQLQuery;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.dao.LoginDao;
import com.enetiy.Logins;
import com.enetiy.Userinfo;
import com.enetiy.Users;
public class LoginDaoImpl extends HibernateDaoSupport implements LoginDao {

	@Override
	public Serializable insertUsers(Users u) throws Exception {
	 Serializable s = this.getHibernateTemplate().save(u);	
		return s;
	}
	@Override
	public Serializable insertLogins(Logins l) throws Exception {
		Serializable save = this.getHibernateTemplate().save(l);
		return save;
	}
	@Override
	public Users selectUserId(int id) throws Exception {
		Users users = this.getHibernateTemplate().get(Users.class, id);
		return users;
	}
	@Override
	public void updateUser(Users u) throws Exception {
		this.getHibernateTemplate().update(u);		
	}
	@Override
	public List<Users> selectUserList(String [] userid) throws Exception {
		String sql = "from Users where id=?";
		for (int i = 0; i < userid.length-1; i++) {
			sql=sql+" or id=?";
		}
		Query query = this.getSession().createQuery(sql);
		System.out.println(userid.length);
		for (int i = 0; i < userid.length; i++) {
			System.out.println(userid[i]);
			query.setParameter(i, Integer.parseInt(userid[i]));
		}
		List<Users> list = query.list();
		return list;
	}
	@Override
	public Users selectUser(String soleCode) throws Exception {
		Query query = this.getSession().createQuery("from Users where sole_code=?");
		query.setParameter(0, soleCode);
		List <Users>list = query.list();
		return list.isEmpty()?null:list.get(0);
	}
	@Override
	public Users selectUser(int niunum) throws Exception {
		Query query = this.getSession().createQuery("from Users where niu_num=?");
		query.setParameter(0, niunum);
		List <Users>list = query.list();
		return list.isEmpty()?null:list.get(0);
	}

	
}
